package njupt.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import njupt.vo.User;
import factory.DAOFactory;

public class LoginServlet extends HttpServlet {
   public void doGet(HttpServletRequest req,HttpServletResponse resp)
              throws ServletException,IOException{
	    req.setCharacterEncoding("GB2312");
		resp.setCharacterEncoding("GB2312");
		resp.setContentType("text/html;charset=GB2312");
		HttpSession session=req.getSession(true);
		String action=req.getParameter("action");
		List<String> info=new ArrayList<String>();
		//List<String> names=new ArrayList<String>();
		
//【】【】登录【】【】
		if(action==null){return;}		
	  if(action.equals("denglu")){	
		      
	         String path="houtai.jsp";
	         String path1="Newindex_logined.jsp";
	         String path2="tousuHander.jsp";
	        String path3="NewTop.jsp";
	         String id=req.getParameter("id");
	         String password=req.getParameter("password");
	       //  System.out.println("id1:"+id);
	         //System.out.println("mima:"+password);

	     
	        if(id==null||"".equals(id)){
		        info.add("12345");
	        }
	        if(password==null||"".equals(password)){
		        info.add("121423423");
	        }
	 
	        if(info.size()==0){
		       System.out.println("mima:");
		       User user = new User();
		       user.setId(id);
		       user.setPassword(password);
		       System.out.println("mima:");
		    try{
			   if(DAOFactory.getUserDAOinstance().checkusers(user)){
				  info.add("欢迎进入数据库");
				 
				  String uname=user.getName();	
				  Integer type=user.getType();
				  session.setAttribute("uname", uname);
				  session.setAttribute("realname", user.getRealname());
				  session.setAttribute("userid", user.getId());	
				  session.setAttribute("phone", user.getPhone());
				  session.setAttribute("address", user.getAddress());
				  session.setAttribute("zipcode", user.getZipcode());
				  session.setAttribute("email", user.getEmail());                           //此处要优化  session占用太多，占用内存
				
				  if(type==0){req.getRequestDispatcher(path1).forward(req, resp);}
				  else if(type==2){req.getRequestDispatcher(path2).forward(req, resp);}
				  else{req.getRequestDispatcher(path).forward(req, resp);}
				  
			  }else{
				  info.add("账号密码错误，请重新登录");
				  System.out.println("数据库未登录");
				  req.setAttribute("info", info);
				  req.getRequestDispatcher(path3).forward(req, resp); 
			  }
		    }catch(Exception e){
			  e.printStackTrace();
		  }
	  }
	 
//	  req.getRequestDispatcher(path1).forward(req, resp);
    }
//【】【】注销【】【】	  
	  else if(action.equals("zhuxiao")){
		  System.out.println("zhuxiao"+session.getAttribute("uname"));
		  req.getSession(true).invalidate();
		 // System.out.println("zhuxiao"+session.getAttribute("uname"));
		  req.getRequestDispatcher("Newindex.jsp").forward(req, resp);
		  
	  }
//【】【】【】注册【】【】	  
	  else if(action.equals("zhuce")){
		  String id=req.getParameter("id");
		  String password=req.getParameter("password");
		  String uname=req.getParameter("name");
		  session.setAttribute("uname", uname);
		  session.setAttribute("userid", id);
		  User user=new User();
		  user.setId(id);
		  user.setPassword(password);
		  user.setName(uname);
		  
		 try{
			 if(DAOFactory.getUserDAOinstance().insertuser(user)==1){
				 info.add("已经成功注册");
				 req.setAttribute("info", info);
				 
				 req.getRequestDispatcher("register.jsp").forward(req, resp);
			 }
		 }catch(Exception e){
			  e.printStackTrace();
		 }
	   }
	  else if(action.equals("userinfo")){
		  String userid=(String) session.getAttribute("userid");
		  String realname=req.getParameter("realname");
		  String phone=req.getParameter("phone");
		  String address=req.getParameter("address");
		  String zipcode=req.getParameter("zipcode");
		  String email=req.getParameter("email");
	     System.out.println("zipcode【】【】"+email);
	   try{
		if(DAOFactory.getUserDAOinstance().updateuserinfo(realname, phone, address, zipcode, email, userid)==1)
		    { 
		   session.setAttribute("realname", realname);
		   session.setAttribute("phone", phone);
		   session.setAttribute("email", email);
		   session.setAttribute("zipcode", zipcode);
		   session.setAttribute("address", address); 
		   }
	   }catch(Exception e){
		   e.printStackTrace();
		   }
	       resp.sendRedirect("buy1.jsp");
	  } 
	  
	  else if(action.equals("checkexist")){
		  PrintWriter out=resp.getWriter();
		  System.out.print("checkexist");
		  int userid=Integer.parseInt(req.getParameter("userid"));
		  try {
			if(DAOFactory.getUserDAOinstance().checkuserexit(userid)!=0){
				out.print("true");
				
			}
			else {out.print("false");}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		  
	  }
	  else if(action.equals("tousuForward")){
		  int type=Integer.parseInt(req.getParameter("type"));
	      int userid= Integer.parseInt((String)session.getAttribute("userid"));
		  System.out.println("type: "+type);
			 List<String[]> tousuForward=new ArrayList<String[]>();
		  try{
			  tousuForward= DAOFactory.getPingJiaDAOinstance().select(type);
			  System.out.println("tousuForward.size(): "+tousuForward.size());
			 req.setAttribute("tousuForward", tousuForward);      //注意这里是从104来判断的  以后扩展的时候要更改
			 
		  }catch(Exception e){
			  e.printStackTrace();
		  }
		  if(userid!=104){ req.getRequestDispatcher("tousuForwardShow.jsp").forward(req, resp);}
		  else{req.getRequestDispatcher("tousuHanderShow.jsp").forward(req, resp);}
	  } 
	  
	  else if(action.equals("Forward")){
    	  int orderid=Integer.parseInt(req.getParameter("orderid"));
    	  int status=Integer.parseInt(req.getParameter("status"));
    	  try{
    		  if(DAOFactory.getPingJiaDAOinstance().updateTousuStatus(orderid, status)!=0){
    			  resp.sendRedirect("LoginServlet?action=tousuForward&type=0");
    		  }
    	  }catch(Exception e){
    		  e.printStackTrace();
    	  }
      }
	  
	  else if(action.equals("tousuWriting")){
		  List<String> selectTousuinfoToHander=new ArrayList<String>();
		  int orderid=Integer.parseInt(req.getParameter("orderid"));
		  try{
			  selectTousuinfoToHander=DAOFactory.getPingJiaDAOinstance().selectTousuinfoToHander(orderid);
			  req.setAttribute("selectTousuinfoToHander", selectTousuinfoToHander);
			  req.getRequestDispatcher("tousuHanderSubmit.jsp").forward(req, resp);
    			  //resp.sendRedirect("tousuHanderSubmit.jsp");
    		  
    	  }catch(Exception e){
    		  e.printStackTrace();
    	  }
	  }
	  
	  else if(action.equals("TousuChuli")){
		   int orderid=Integer.parseInt(req.getParameter("orderid"));
		   String tousuchulixinfo=req.getParameter("textarea");
		   try{
			   DAOFactory.getPingJiaDAOinstance().insertTousuChuli(orderid, tousuchulixinfo);
			   System.out.println("insertTousuChuli"+tousuchulixinfo);
			   resp.sendRedirect("LoginServlet?action=tousuForward&type=1");
		   }catch(Exception e){
			   e.printStackTrace();
		   }
	   }
	  else if(action.equals("custmerservice")){
		  int userid=Integer.parseInt((String)session.getAttribute("userid"));
		  List<Integer> selectOrderidForCustmerShowTousu=new ArrayList<Integer>();
		  System.out.println("[][][custmerservice]");
		  try{
			  selectOrderidForCustmerShowTousu=DAOFactory.getPingJiaDAOinstance().selectOrderidForCustmerShowTousu(userid);
			  req.setAttribute("selectOrderidForCustmerShowTousu", selectOrderidForCustmerShowTousu);
			  req.getRequestDispatcher("CustmerService.jsp").forward(req, resp);
		  }catch(Exception e){e.printStackTrace();}
		  
		  
	  }
	  else if(action.equals("custmershowtousu")){
		  List<String> selectTousuByCustmer=new ArrayList<String>();
		  int orderid=Integer.parseInt(req.getParameter("orderid"));
		  
		  try{
			  selectTousuByCustmer=DAOFactory.getPingJiaDAOinstance().selectTousuByCustmer(orderid);
			  req.setAttribute("selectTousuByCustmer", selectTousuByCustmer);
			  req.getRequestDispatcher("CustmerService_tousu.jsp").forward(req, resp);
			  
		  }catch(Exception e){ e.printStackTrace();}
	  }
   }
   
  public void doPost(HttpServletRequest req,HttpServletResponse resp)
             throws ServletException,IOException{
	      this.doGet(req, resp);
  }
}   



